#include <Acceptor.h>
#include <EventLoop.h>
#include <EventLoopThreadPool.h>
#include <mutex>
#include <condition_variable>
#include <Logger.h>
#include <Buffer.h>
#include <EventLoopThread.h>

// 1、创建全局异步logger对象
std::unique_ptr<AsyncLogger::AsyncLogger> g_logger(new AsyncLogger::AsyncLogger("./", "test", 1024 * 1024 * 1024, false));

// 2、创建logger回调函数
void asyncLog(const char *logline, int len)
{
    g_logger->append(logline, len);
    g_logger->append("\n", 1);
}

std::mutex g_mutex;
std::condition_variable g_cond;
EventLoop *g_loop;

int main(int argc, char *argv[])
{
    // 3、设置日志级别和logger输出回调函数
    AsyncLogger::Logger::setLogLevel(AsyncLogger::LogLevel::TRACE);
    AsyncLogger::Logger::setOutput(asyncLog);
    // 4、创建全局asyncLogger对象
    std::string name = argv[0];
    g_logger->start();
    // bool isInit = false;
    // EventLoopThread loopThread(name, [&](EventLoop *loop)
    //                            {
    //                             std::lock_guard<std::mutex> lock(g_mutex);
    //                             LOG_DEBUG << name; 
    //                             isInit = true;
    //                             g_cond.notify_one(); });
    // loopThread.startLoop();
    // std::unique_lock<std::mutex> lock(g_mutex);
    // g_cond.wait(lock, [&]()
    //             { return isInit; });
    EventLoop loop;
    EventLoopThreadPool pool(&loop);
    pool.start(3,[](EventLoop* loop){
        std::cout << "thread " << std::this_thread::get_id() << " init" << std::endl;
    });
    for(int i=0;i<3;++i){
        pool.getNextLoop()->runInLoop([](){
            std::cout << "hello world" << std::endl;
        });
    }
    int a = 10;
    for(auto _loop:pool.getAllLoops()){
        _loop->runInLoop([=](){
            std::cout << "a = " << a << std::endl;
        });
        a+=20;
    }

    char c;
    std::cin >> c; 
}